﻿using CPPEI.Coolzon.Event.NCL;
using CPPEI.Coolzon.Service.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace CPPEI.Coolzon.UI.Views
{
    public partial class SelectSolutionView
    {
        [Common.Subscribe]
        public void OnReceiveNCLGetSolutionListEvent(NCLGetSolutionListEvent @event)
        {
            if (@event.State == NCLEventState.Finished)
            {
                // 获取成功
                this.Dispatcher.Invoke(() =>
                {
                    wrapPanel.Visibility = System.Windows.Visibility.Visible;
                    _ctlPanelRetry.Visibility = System.Windows.Visibility.Hidden;

                    wrapPanel.Children.Clear();

                    foreach (SolutionItem item in @event.Result.SolutionList)
                    {
                        if (!string.IsNullOrWhiteSpace(_preSolutionName) && _preSolutionName.Equals(item.Name))
                        {
                            continue;
                        }
                        Image image = (Image)Resources["image"];

                        Grid grid = new Grid();
                        grid.Width = wrapPanel.Width / 3;
                        grid.RowDefinitions.Add(new RowDefinition());
                        grid.RowDefinitions.Add(new RowDefinition());

                        Border border = new Border();
                        border.BorderThickness = new Thickness(2, 2, 2, 2);
                        border.Background = new ImageBrush(image.Source);
                        border.Height = 188;
                        Grid.SetRow(border, 0);


                        TextBlock label = new TextBlock();
                        label.Text = item.Caption;
                        label.FontWeight = FontWeights.Bold;
                        label.FontSize = 20;
                        label.HorizontalAlignment = HorizontalAlignment.Left;
                        label.VerticalAlignment = VerticalAlignment.Top;
                        label.Foreground = new SolidColorBrush(Colors.White);
                        label.HorizontalAlignment = HorizontalAlignment.Center;
                        label.TextWrapping = TextWrapping.Wrap;
                        Grid.SetRow(label, 1);

                        grid.Children.Add(border);
                        grid.Children.Add(label);

                        Button btnSolution = new Button();
                        btnSolution.Content = grid;
                        btnSolution.Click += btnSolution_Click;
                        btnSolution.Style = (Style)this.Resources["s_btn"];
                        btnSolution.Tag = item;

                        wrapPanel.Children.Add(btnSolution);
                    }
                });
            }
            else if (@event.State == NCLEventState.Failure)
            {
                this.Dispatcher.Invoke(() =>
                {
                    // 获取失败
                    wrapPanel.Visibility = System.Windows.Visibility.Hidden;
                    _ctlPanelRetry.Visibility = System.Windows.Visibility.Visible;
                });
            }
        }

    }
}
